<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ArticleDao
 *
 * @author Binh
 */
class NewsDao {
    public static function getNews() {
        $ds = array();
        $sql = "select * from news where deleted=0";
        $helper = new MySqlDataAccessHelper;

        //$helper->open();
        $rs = $helper->executeQuery($sql);
        if (FALSE === $rs) {
            echo mysql_error();
            return $ds;
        } else {
            while ($row = mysql_fetch_array($rs)) {
                $obj = new News($row["id"],$row["name"],$row["content"],$row["time"],$row["views"],$row["thumbnail"]);
                array_push($ds, $obj);
            }
            //$helper->close();
            return $ds;
        }
    }
    
    public static function getNew($id) {
        $obj = NULL;
        $sql = sprintf("select * from news where deleted=0 and id=%d", $id);
        $helper = new MySqlDataAccessHelper;

        //$helper->open();
        $rs = $helper->executeQuery($sql);
        if (FALSE === $rs) {
            echo mysql_error();
            return $ds;
        } else {
            if ($row = mysql_fetch_array($rs)) {
                $obj = new News($row["id"],$row["name"],$row["content"],$row["time"],$row["views"],$row["thumbnail"]);
            }
            //$helper->close();
            return $obj;
        }
    }
    
    public static function getCommonNews($count) {
        $ds = array();
        $sql = sprintf("select * from news where deleted=0 order by views ASC limit %d",$count);
        $helper = new MySqlDataAccessHelper;

        //$helper->open();
        $rs = $helper->executeQuery($sql);
        if (FALSE === $rs) {
            echo mysql_error();
            return $ds;
        } else {
            while ($row = mysql_fetch_array($rs)) {
                $obj = new News($row["id"],$row["name"],$row["content"],$row["time"],$row["views"],$row["thumbnail"]);
                array_push($ds, $obj);
            }
            //$helper->close();
            return $ds;
        }
    }
    public static function getAnotherNews($id) {
        $ds = array();
        $sql = sprintf("select * from news where deleted=0 and id <>%d order by time ASC limit 5",$id);
        $helper = new MySqlDataAccessHelper;

        //$helper->open();
        $rs = $helper->executeQuery($sql);
        if (FALSE === $rs) {
            echo mysql_error();
            return $ds;
        } else {
            while ($row = mysql_fetch_array($rs)) {
                $obj = new News($row["id"],$row["name"],$row["content"],$row["time"],$row["views"],$row["thumbnail"]);
                array_push($ds, $obj);
            }
            //$helper->close();
            return $ds;
        }
    }
    
    
    public static function insertNew($name, $content, $thumbnail) {
        $sql = sprintf("INSERT INTO news (name, content, thumbnail, time) Values('%s', '%s', '%s',  now())", $name, $content, $thumbnail);
        $helper = new MySqlDataAccessHelper;

        $helper->open();
        $helper->executeUpdate($sql);
        $helper->close();
    }
}

?>
